$(function(){
    $("[rel-action=form]").submit(function() {
        var funcCallback = $(this).attr("rel-callback");
        var funcSuccess = $(this).attr("rel-success") || 'successCallback(response.message, response.data)';
        var funcError = $(this).attr("rel-error") || 'errorCallback(response.message, response.data)';
        var csrfContent = $("meta[name=csrf-token]").attr('content');
        var beforeFun = $(this).attr("rel-before") || "";

        top.window.isTop = $(this).attr("rel-isTop") || false;

        if ( beforeFun ) {
            var beforeFunRet = eval(beforeFun)();//console.log(beforeFunRet)
            if ( typeof  beforeFunRet === "object") {
                if ( !beforeFunRet.status ) {
                    loadingClose();
                    alertError(beforeFunRet.message);
                    return false;
                }
            } else {
                if ( !beforeFunRet ) {
                    loadingClose();
                    return false;
                }
            }
        }
        $("[rel-mark]").text("")
        loadingShow();
        $(this).ajaxSubmit({
            dataType: 'json',
            headers: {"X-CSRF-Token":csrfContent},
            success: function (response) {
                loadingClose();
                if ( funcCallback ) {
                    eval(funcCallback)
                }
                if ( response.status == 1 ) {
                    eval(funcSuccess);
                } else {
                    eval(funcError);
                }
            },
            error: function(response){
                loadingClose();
                alertError("系统出错：" + response.statusText)
            }
        });
        return false;
    });


    $("body").on("click", "[rel-action=prompt]", function(){
        var url = $(this).attr("href");
        var title = $(this).attr("rel-title") || "默认对话框";
        var msg = $(this).attr("rel-msg") || "";
        var id = $(this).attr("rel-id") || 0;
        var csrfContent = $("meta[name=csrf-token]").attr('content');

        var funcCallback = $(this).attr("rel-callback") || "";
        var funcSuccess = $(this).attr("rel-success") || 'successCallback(response.message, response.data)';
        var funcError = $(this).attr("rel-error") || 'errorCallback(response.message, response.data)';
        var requestData = $(this).attr('rel-params') || {};
        var beforeFun = $(this).attr("rel-before") || "";

        if ( beforeFun ) {
            eval(beforeFun);
        }

        alertWarning(title, msg, function(){
            loadingShow();
            if ( typeof requestData !== "object" ) {
                /*console.log(requestData)
                console.log(typeof requestData)*/
                requestData = JSON.parse(requestData)
            }

            if ( id ) {
                requestData['id'] = id;
            }
            $.ajax({
                type:"POST",
                url: url,
                data: requestData,
                headers:{"X-CSRF-Token":csrfContent},
                dataType:'json',
                success: function (response) {
                    loadingClose();
                    if ( funcCallback ) {
                        eval(funcCallback)
                    }
                    if ( response.status == 1 ) {
                        eval(funcSuccess)
                    } else {
                        eval(funcError)
                    }
                }
            })
        });

        return false;
    });

    $("body").on("click", "[rel-action=del]", function(){
        var url = $(this).attr("href");
        var title = $(this).attr("rel-title") || "你确定要删除吗?";
        var msg = $(this).attr("rel-msg") || "";
        var csrfContent = $("meta[name=csrf-token]").attr('content');
        var id = $(this).attr("rel-id") || 0;

        var funcSuccess = $(this).attr("rel-success") || 'successCallback(response.message, response.data)';
        var funcError = $(this).attr("rel-error") || 'errorCallback(response.message, response.data)';
        var requestData = $(this).attr('rel-params') || {};
        if ( typeof requestData !== "object" ) {
            requestData = JSON.parse(requestData)
        }

        if ( id ) {
            requestData['id'] = id;
        }
        alertWarning(title, msg, function(){
            loadingShow();

            $.ajax({
                type:"GET",
                url: url,
                data:requestData,
                headers:{"X-CSRF-Token":csrfContent},
                dataType:'json',
                success: function (response) {
                    loadingClose();
                    if ( response.status == 1 ) {
                        eval(funcSuccess);
                    } else {
                        eval(funcError);
                    }
                }
            })
        });

        return false;
    });
    $("body").on("click", "[rel-action=open]", function () {
        var width = $(this).attr('rel-width') || "80%"
        var height = $(this).attr('rel-height') || "70%"
        var title = $(this).attr('rel-title');
        var type = $(this).attr('rel-type') || 2;
        var before = $(this).attr("rel-before") || null;
        var callbackEnd = $(this).attr('rel-callback-end') || "";
        var callbackSuccess = $(this).attr('rel-callback-success') || "";
        var maxmin = $(this).attr('rel-maxmin') && $(this).attr('rel-maxmin')=='false' ? false : true;
        var nModule = $(this).attr("rel-mModule") || 0;
        var relIdx = $(this).attr("rel-idx")||'';

        if ( before ) {
            var beforeFunRet = eval(before)($(this));//console.log(beforeFunRet)
            if ( typeof  beforeFunRet === "object") {
                if ( !beforeFunRet.status ) {
                    loadingClose();
                    alertError(beforeFunRet.message);
                    return false;
                }
            } else {
                if ( !beforeFunRet ) {
                    loadingClose();
                    return false;
                }
            }
        }
        if(typeof top.window.isTop == "undefined"){
            top.window.isTop = false;
        }
        top.window.isTop = $(this).attr("rel-isTop") || false;
        close=1;
        if(title=="false") {
            title=false;
            close=0;
        }
        if(height=="100%"){
            top.$('nav.navbar-static-top').css({'z-index':'999'});
        }
        var init = {
            type: parseInt(type),
            title: title,
            shadeClose: false,
            shade: 0.3,
            maxmin: false, //开启最大化最小化按钮
            area: [width, height],
            closeBtn:close,
            content: $(this).attr("href"),
            success : function (layero, index) {
                var _layer = top.document.getElementById("layui-layer-iframe" + index);

                if ( typeof _layer.contentWindow == "undefined" ) {
                    return ;
                }
                _layer = _layer.contentWindow;
                if(relIdx){
                    _layer.$('body').attr('rel-idx', relIdx);
                    top.window.relIdx = relIdx;
                }/*
                if ( nModule == 0 || !nModule ) {
                    return false;
                }*/
                //自动根据索引生成模块id
                var module_id = $("#module_id").val();

                //自动根据索引生成模块id
                if ( !module_id || typeof module_id == "undefined" ) {
                    _layer.$("#module_id").val(index);//console.log("yess23")
                    //$("#module_id").val(index);
                    if ( typeof top.window.mod === "undefined" ) {
                        top.window.mod = {};
                    }
                    top.window.mod[index] = index;
                } else {
                    if ( typeof top.window.mod === "undefined" ) {
                        top.window.mod = {};
                    }
                    _layer.$("#module_id").val(module_id)
                    top.window.mod[module_id] = module_id;
                }
            }
        };

        if ( $(this).attr("rel-offset") ) {
            init['offset'] = $(this).attr("rel-offset");
        }

        if ( $(this).attr('rel-iframe-id') ) {
            init['id'] = $(this).attr('rel-iframe-id');
        }

        if ( callbackEnd ) {
            init["end"] = function () {
                eval(callbackEnd + "()");
            };
        }

        if ( callbackSuccess ) {
            init["success"] = function (index, layero) {
                eval(callbackSuccess + "(index, layero)");
            };
        }

        top.layer.open(init);

        return false;
    });

    $("body").on("click", "[rel-action=url]", function(){
        var url = $(this).data("url");
        if ( !url ) {
            url = $(this).attr("href")
        }
        var csrfContent = $("meta[name=csrf-token]").attr('content');
        var requestData = $(this).attr('rel-params') || {};
        var method = $(this).attr("rel-method") || "GET";
        if(typeof top.window.isTop == "undefined"){
            top.window.isTop = false;
        }
        top.window.isTop = $(this).attr("rel-isTop") || false;
        if( typeof(requestData) == 'string' ){
            requestData = JSON.parse(requestData);
        }
        loadingShow();
        $.ajax({
            type:method,
            url: url,
            data:requestData,
            headers:{"X-CSRF-Token":csrfContent},
            dataType:'json',
            success: function (response) {
                loadingClose();
                if ( response.status == 1 ) {
                    successCallback(response.message, response.data)
                } else {
                    errorCallback(response.message, response.data)
                }
            }
        });

        return false;
    });

    $("body").on("click", "[rel-action=input]", function(){
        var csrfContent = $("meta[name=csrf-token]").attr('content');
        var title = $(this).attr("rel-title");
        var formType = $(this).attr("rel-type") || 3;
        var method = $(this).attr("rel-method") || "post";
        var url = $(this).attr("href");
        var name = $(this).attr("rel-name");
        var params = $(this).attr("rel-params");
        var value = $(this).attr("rel-val")
        if ( params ) {
            data = JSON.parse(params);
        } else {
            var data = {};
        }
        data[name] = "";
        layer.prompt({title: title, formType: formType, value:value}, function(pass, index){
            layer.close(index);
            data[name] = pass;
            loadingShow();
            $.ajax({
                type : method,
                url : url,
                data : data,
                headers:{"X-CSRF-Token":csrfContent},
                dataType:'json',
                success: function (response) {
                    loadingClose();
                    if ( response.status == 1 ) {
                        successCallback(response.message, response.data)
                    } else {
                        errorCallback(response.message, response.data)
                    }
                }
            })
        });
        return false;
    });

    $("body").on("change", "[rel-action=url]", function(){
        var url = $(this).data("url");
        var csrfContent = $("meta[name=csrf-token]").attr('content');
        var requestData = $(this).attr('rel-params') || {};
        var method = $(this).attr("rel-method") || "GET";
        if(typeof(requestData)=='string'){
            requestData = JSON.parse(requestData);
        }
        loadingShow();
        $.ajax({
            type:method,
            url: url,
            data:requestData,
            headers:{"X-CSRF-Token":csrfContent},
            dataType:'json',
            success: function (response) {
                loadingClose();
                if ( response.status == 1 ) {
                    successCallback(response.message, response.data)
                } else {
                    errorCallback(response.message, response.data)
                }
            }
        });

        return false;
    });

    $("[rel-action=checkboxAll]").click(function () {
        var mark = $(this).attr("rel-main-mark");
        var items = $("[rel-mark=" + mark + "]");

        if ( $("[rel-mark=" + mark + "]:checked").length === items.length ) {
            items.removeAttr("checked");
            $(this).removeAttr("checked");;
        } else {
            $(this).prop("checked");
            items.prop("checked", "checked")
        }
    });

    $("[rel-action=checkbox]").click(function () {
        var mark = $(this).attr("rel-mark");
        var main = $("[rel-main-mark=" + mark + "]");
        if ( $("[rel-mark=" + mark + "]:checked").length === $("[rel-mark=" + mark + "]").length ) {
            main.prop("checked", "checked")
        } else {
            main.removeAttr("checked");
        }
    });
    if ( $("[rel-action=checkbox]").length > 0 ) {
        $.each($("[rel-action=checkbox]"), function(){
            var mark = $(this).attr("rel-mark");
            if ( mark ) {
                var main = $("[rel-main-mark=" + mark + "]");
                if ( $("[rel-mark=" + mark + "]:checked").length === $("[rel-mark=" + mark + "]").length ) {
                    main.prop("checked", "checked")
                } else {
                    main.removeAttr("checked");
                }
            }
        })
    }

    $(".checked-with-tr").click(function () {
        $(this).find(".checked-td").click();
    });

});